﻿Imports System.Security.Cryptography

Public Class Encryption

    Public Const strSharedAESPassword = "fn2i0hnv20hv-2inhv-2inhvi20bhv02ibhv2i-hbv2-i9hv2-9bh-94bh24-9bv"

    Public Shared Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String

        Dim AES As AesManaged = Nothing
        Dim Hash_AES As MD5CryptoServiceProvider = Nothing
        Dim DESEncrypter As ICryptoTransform = Nothing
        Dim encrypted As String = ""
        Dim hash(31) As Byte
        Dim temp As Byte()

        Dim Buffer As Byte()

        Try

            AES = New AesManaged
            Hash_AES = New MD5CryptoServiceProvider

            temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = CipherMode.ECB
            DESEncrypter = AES.CreateEncryptor
            Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

        Catch ex As Exception

        Finally

            AES_Encrypt = encrypted

            If Not AES Is Nothing Then

                AES.Dispose()

            End If

            AES = Nothing

            If Not Hash_AES Is Nothing Then

                Hash_AES.Dispose()

            End If

            Hash_AES = Nothing

            If Not DESEncrypter Is Nothing Then

                DESEncrypter.Dispose()

            End If

            DESEncrypter = Nothing

        End Try

    End Function

    Public Shared Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String

        Dim AES As AesManaged = Nothing
        Dim Hash_AES As MD5CryptoServiceProvider = Nothing
        Dim DESDecrypter As ICryptoTransform = Nothing
        Dim decrypted As String = ""
        Dim hash(31) As Byte
        Dim temp As Byte()
        Dim Buffer As Byte()

        Try

            AES = New AesManaged
            Hash_AES = New MD5CryptoServiceProvider

            temp = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = CipherMode.ECB
            DESDecrypter = AES.CreateDecryptor
            Buffer = Convert.FromBase64String(input)
            decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

        Catch ex As Exception

        Finally

            AES_Decrypt = decrypted

            If Not AES Is Nothing Then

                AES.Dispose()

            End If

            AES = Nothing

            If Not Hash_AES Is Nothing Then

                Hash_AES.Dispose()

            End If

            Hash_AES = Nothing

            If Not DESDecrypter Is Nothing Then

                DESDecrypter.Dispose()

            End If

            DESDecrypter = Nothing

        End Try

    End Function

End Class
